package com.evan.review.architecture.components.workers;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
 * 简单的Worker示例
 * 用于演示WorkManager的基本用法
 */
public class SimpleWorker extends Worker {

    private static final String TAG = "SimpleWorker";

    public SimpleWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        // 获取输入数据
        String taskType = getInputData().getString("task_type");
        long timestamp = getInputData().getLong("timestamp", 0);

        Log.d(TAG, "开始执行任务: " + taskType + ", 开始时间: " + timestamp);

        try {
            // 模拟耗时操作
            for (int i = 0; i < 5; i++) {
                Log.d(TAG, "任务进度: " + (i + 1) + "/5");
                Thread.sleep(1000);
            }

            // 创建输出数据
            long completionTime = System.currentTimeMillis();
            Data outputData = new Data.Builder()
                    .putString("result", "任务 '" + taskType + "' 成功完成")
                    .putLong("completion_time", completionTime)
                    .build();

            Log.d(TAG, "任务完成: " + taskType + ", 完成时间: " + completionTime);

            // 返回成功结果
            return Result.success(outputData);

        } catch (InterruptedException e) {
            Log.e(TAG, "任务执行被中断", e);

            // 返回失败结果
            return Result.failure();
        }
    }
} 